package com.chhuang.system.service.impl;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.system.bean.ChMenuQueryBean;
import com.chhuang.system.mapper.ChMenuMapper;
import com.chhuang.system.po.ChMenu;
import com.chhuang.system.po.ChMenuExample;
import com.chhuang.system.service.IChMenuService;
import com.chhuang.utils.Assert;

/**
* ChMenu ServiceImpl
* @author ChHuang
* @date Fri Mar 03 13:20:59 CST 2017
*/
@Service
public class ChMenuServiceImpl extends BaseServiceImpl<ChMenuMapper, ChMenu> implements IChMenuService {
	@Autowired
	private ChMenuMapper chMenuMapper;

	@Override
	public QueryBean<ChMenu> seniorQuery(QueryBean<ChMenu> queryBean) throws Exception {
		ChMenuQueryBean poQueryBean = (ChMenuQueryBean)queryBean;
		ChMenuExample example = new ChMenuExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			ChMenuExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getMenuName())){
				criteria.andMenuNameLike("%"+poQueryBean.getMenuName()+"%");
			}
			if(Assert.isValidString(poQueryBean.getUrl())){
				criteria.andUrlLike("%"+poQueryBean.getUrl()+"%");
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getMenuName())){
				example.or().andMenuNameLike("%"+poQueryBean.getMenuName()+"%");
			}
			if(Assert.isValidString(poQueryBean.getUrl())){
				example.or().andUrlLike("%"+poQueryBean.getUrl()+"%");
			}
		}
		
		poQueryBean.setItemCount(chMenuMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getPageCount()>=poQueryBean.getPageNum()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset((poQueryBean.getPageNum()-1)*poQueryBean.getPageSize());
				example.setLimit(poQueryBean.getPageSize());
				poQueryBean.setResult(chMenuMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}
	
	@Override
	public List<ChMenu> findAsParent() throws Exception {
		ChMenuExample example = new ChMenuExample();
		example.createCriteria().andAsParentEqualTo(true);
		example.setOrderByClause("menu_order asc");
		return chMenuMapper.selectByExample(example);
	}

	@Override
	public int deleteByParentIdArray(String[] menuIds) throws Exception {
		ChMenuExample example = new ChMenuExample();
		example.createCriteria().andParentIdIn(Arrays.asList(menuIds));
		return chMenuMapper.deleteByExample(example);
	}

	@Override
	public int deleteByMenuIdArray(String[] menuIds) throws Exception {
		ChMenuExample example = new ChMenuExample();
		example.createCriteria().andMenuIdIn(Arrays.asList(menuIds));
		return chMenuMapper.deleteByExample(example);
	}

}